﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormKeyboard : Form
    {
        public FormKeyboard()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormKeyboard_Load(object sender, EventArgs e)
        {
            string keyboard = ModelBO.getParametro(
                                  ModelBO.getParametro(ParametroVO.parametros.Keyboard));
                //"1!~2@~3#~4$~5%~6¨~7&~8*~9(~0)~-_~=+";
               //keyboard += "~q~w~e~r~t~y~u~i~o~p~[{~]}";
               //keyboard += "~a~s~d~f~g~h~j~k~l~ç~\\|~/?";
               //keyboard += "~z~x~c~v~b~n~m~,<~.>~;:";
            
            foreach (string key in keyboard.Split('~'))
            {
                Button myButton = new Button();
                myButton.Name = "buttonKey";
                myButton.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                myButton.Size = new System.Drawing.Size(55, 50);
                myButton.TabIndex = 4;
                myButton.UseVisualStyleBackColor = true;
                if (key.Length > 1)
                {
                    myButton.Text = key[1].ToString() + "\n  " + key[0].ToString();
                }
                else
                {
                    myButton.Text = key[0].ToString();
                }
                myButton.Tag = key;

                myButton.Click += new System.EventHandler(this.buttonKey1_Click);
                flowLayoutPanelKeyboard.Controls.Add(myButton);

            }
            Button myButton2 = new Button();
            myButton2.Name = "buttonSpace";
            myButton2.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            myButton2.Size = new System.Drawing.Size(115, 50);
            myButton2.TabIndex = 4;
            myButton2.Text = "Espaço";
            myButton2.UseVisualStyleBackColor = true;
            myButton2.Click += new System.EventHandler(this.buttonKey1_Click);
            flowLayoutPanelKeyboard.Controls.Add(myButton2);

            textBoxPlaca.Text += "_"; //.PadRight(this.textBoxPlaca.MaxLength, fill);
            this.textBoxPlaca.SelectionStart = textBoxPlaca.Text.Length-1;
            this.textBoxPlaca.SelectionLength = 1;
            currentPos = textBoxPlaca.Text.Length - 1;


        }
        
        private int currentPos = 0;
        private bool caps = false;
        private bool shift = false;

        private void buttonKey1_Click(object sender, EventArgs e)
        {

            if (((Button)sender).Name.Equals("buttonLeft"))
            {
                if (currentPos > 0)
                {
                    currentPos--;
                }
                if (currentPos > 0)
                {
                    if (textBoxPlaca.Text[currentPos].Equals("\r".ToCharArray()[0]) ||
                       textBoxPlaca.Text[currentPos].Equals("\n".ToCharArray()[0]))
                    {
                        buttonKey1_Click(sender, e);
                    }
                }
            }
            else if (((Button)sender).Name.Equals("buttonRight"))
            {
                if (currentPos + 1 < this.textBoxPlaca.Text.Length) //.MaxLength)
                {
                    currentPos++;
                }
                if (textBoxPlaca.Text[currentPos].Equals("\r".ToCharArray()[0]) ||
                   textBoxPlaca.Text[currentPos].Equals("\n".ToCharArray()[0]))
                {
                    buttonKey1_Click(sender, e);
                }
            }
            else if (((Button)sender).Name.Equals("buttonBackSpace"))
            {
                if (currentPos > 0)
                {

                    textBoxPlaca.Text = textBoxPlaca.Text.Substring(0, currentPos-1) + textBoxPlaca.Text.Substring(currentPos);
                    /*char[] texto = this.textBoxPlaca.Text.ToCharArray();
                    texto[currentPos - 1] = fill;
                    textBoxPlaca.Text = new string(texto);*/
                    currentPos--;

                    if (currentPos > 0)
                    {
                        if (textBoxPlaca.Text[currentPos - 1].Equals("\r".ToCharArray()[0]) ||
                            textBoxPlaca.Text[currentPos - 1].Equals("\n".ToCharArray()[0]))
                        {
                            buttonKey1_Click(sender, e);
                        }
                    }
                }
            }
            else if (((Button)sender).Name.Equals("buttonDel"))
            {
                if (textBoxPlaca.Text.Length > currentPos + 1)
                {
                    textBoxPlaca.Text = textBoxPlaca.Text.Substring(0, currentPos) + textBoxPlaca.Text.Substring(currentPos + 1);
                    if (textBoxPlaca.Text[currentPos].Equals("\r".ToCharArray()[0]) ||
                       textBoxPlaca.Text[currentPos].Equals("\n".ToCharArray()[0])){
                        buttonKey1_Click( sender, e);
                    }
                }
                
                /*char[] texto = this.textBoxPlaca.Text.ToCharArray();
                texto[currentPos] = fill;
                textBoxPlaca.Text = new string(texto);
                if (currentPos + 1 < this.textBoxPlaca.MaxLength)
                {
                    currentPos++;
                }*/
            }
            else if (((Button)sender).Name.Equals("buttonDeleteAll"))
            {
                textBoxPlaca.Text = "_"; //.PadRight(this.textBoxPlaca.MaxLength,fill);
                currentPos = 0;
            }
            else if (((Button)sender).Name.Equals("buttonCaps"))
            {
                caps = !caps;
                this.buttonCaps.ForeColor = (caps ? Color.Red : Color.Black);
                return;
            }
            else if (((Button)sender).Name.Equals("buttonShift"))
            {
                shift = !shift;
                this.buttonShift.ForeColor = (shift?Color.Red:Color.Black);
                return;
            }
            else if (currentPos < this.textBoxPlaca.MaxLength)
            {
                //char[] texto=this.textBoxPlaca.Text.ToCharArray();
                string temp =  ((caps || shift) &&!(caps && shift)?((String)((Button)sender).Tag).ToUpper():(String)((Button)sender).Tag);

                if (((Button)sender).Name.Equals("buttonSpace"))
                    temp = " "; //texto[currentPos] = ' ';
                else if (((Button)sender).Name.Equals("buttonEnter"))
                {
                    temp = "\r\n" + (currentPos + 1 < textBoxPlaca.Text.Length ? textBoxPlaca.Text[currentPos].ToString() : ""); //texto[currentPos] = "\r".ToCharArray()[0];
                    
                    //texto[currentPos+1] = "\n".ToCharArray()[0]; 0123_
                }
                else
                    temp = temp[(shift && temp.Length > 1 ? 1 : 0)].ToString(); //texto[currentPos] = temp[(shift && temp.Length > 1 ? 1 : 0)];

                textBoxPlaca.Text = textBoxPlaca.Text.Substring(0, currentPos) + temp + (textBoxPlaca.Text.Length> currentPos+1? textBoxPlaca.Text.Substring(currentPos + 1):"_");
                currentPos += temp.Length;

                //textBoxPlaca.Text = new string(texto);
                //if (currentPos + 1 < this.textBoxPlaca.MaxLength)
                    //currentPos++;

            }
            this.textBoxPlaca.SelectionStart = currentPos;
            this.textBoxPlaca.SelectionLength = 1;

            shift = false;
            this.buttonShift.ForeColor = Color.Black;
        }


        public void setText(string text)
        {
            this.textBoxPlaca.Text = text;
        }
        public string getText()
        {
            return this.textBoxPlaca.Text.Substring(0,this.textBoxPlaca.Text.Length-1);
        }

        private void buttonEnviar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
